package com.atguigu.gmall.realtime.publisher.controller;

import com.atguigu.gmall.realtime.publisher.beans.TradeProvinceOrderAmount;
import com.atguigu.gmall.realtime.publisher.service.TradeStatsService;
import com.atguigu.gmall.realtime.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2023/3/22
 * 交易域统计Controller
 * @Controller : 如果类中的方法返回的是字符串，会进行页面的跳转;如果不想进行页面的跳转，可以在方法上加@ResponseBody
 * @RestController ：如果类中的方法返回的是字符串，直接响应给客户端
 * @RestController = @Controller + @ResponseBody
 * @RequestMapping("/gmv"): 拦截客户端请求，并将请求交给标注的方法进行处理
 */
@RestController
public class TradeStatsController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        BigDecimal gmv = tradeStatsService.getGMV(date);
        String json = "{\"status\": 0,\"data\": " + gmv + " }";

        return json;
    }

    /*@RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceAmountList.size(); i++) {
            TradeProvinceOrderAmount provinceOrderAmount = provinceAmountList.get(i);
            jsonB.append("{\"name\": \"" + provinceOrderAmount.getProvinceName() + "\"," +
                "\"value\": " + provinceOrderAmount.getOrderAmount() + "}");
            if(i < provinceAmountList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("],\"valueName\": \"交易额\"}}");
        return jsonB.toString();
    }*/
    @RequestMapping("/province")
    public Map getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatsService.getProvinceAmount(date);

        Map resMap = new HashMap();
        resMap.put("status",0);
        Map dataMap = new HashMap();
        List dataList = new ArrayList();
        for (TradeProvinceOrderAmount orderAmount : provinceAmountList) {
            Map objMap = new HashMap();
            objMap.put("name",orderAmount.getProvinceName());
            objMap.put("value",orderAmount.getOrderAmount());
            dataList.add(objMap);
        }
        dataMap.put("mapData",dataList);
        dataMap.put("valueName","交易额");

        resMap.put("data",dataMap);
        return resMap;
    }
}
